//
//  NSArray+Extension.m
//  ICome
//
//  Created by zhang_rongwu on 16/3/3.
//  Copyright © 2016年 XianZhuangGuo. All rights reserved.
//

#import "NSArray+Extension.h"
#import <objc/runtime.h>
#import "NSObject+Extension.h"

@implementation NSArray (Extension)
+ (void)load {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originalMethod = class_getClassMethod(self, @selector(arrayWithObjects:count:));
        Method swizzledMethod = class_getClassMethod(self, @selector(safe_arrayWithObjects:count:));
        method_exchangeImplementations(originalMethod, swizzledMethod);
        });
}

+ (instancetype)safe_arrayWithObjects:(const id [])objects count:(NSUInteger)cnt {
    id nObjects[cnt];
    int i=0, j=0;
    for (; i<cnt && j<cnt; i++) {
        if (objects[i]) {
            nObjects[j] = objects[i];
            j++;
        }else{
            NSLog(@"字数组面 存在了 nil数据！！！");
            NSLog(@"index 为 %d",i);
        }
    }
    
    return [self safe_arrayWithObjects:nObjects count:j];
}

//+ (void)load
//{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        @autoreleasepool {
//            [objc_getClass("__NSArray0") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(emptyObjectIndex:)];
//            [objc_getClass("__NSArrayI") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(arrObjectIndex:)];
//            [objc_getClass("__NSArrayM") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(mutableObjectIndex:)];
//            [objc_getClass("__NSArrayM") swizzleMethod:@selector(insertObject:atIndex:) swizzledSelector:@selector(mutableInsertObject:atIndex:)];
//            [objc_getClass("__NSArrayM") swizzleMethod:@selector(integerValue) swizzledSelector:@selector(replace_integerValue)];
//        }
//    });
//}
//
//- (id)emptyObjectIndex:(NSInteger)index{
//    return nil;
//}
//
//- (id)arrObjectIndex:(NSInteger)index{
//    if (index >= self.count || index < 0) {
//        return nil;
//    }
//    return [self arrObjectIndex:index];
//}
//
//- (id)mutableObjectIndex:(NSInteger)index{
//    if (index >= self.count || index < 0) {
//        return nil;
//    }
//    return [self mutableObjectIndex:index];
//}
//
//- (void)mutableInsertObject:(id)object atIndex:(NSUInteger)index{
//    if (object) {
//        [self mutableInsertObject:object atIndex:index];
//    }
//}
//
//- (NSInteger)replace_integerValue {
//    return 0;
//}

@end
